package com.yc.ui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;

import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.Idao.LoginIDao;
import com.yc.dao.LoginDao;
import com.yc.ui.admin.Manager;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

/***
 * 快速构建界面 
 * company 源辰信息
 * @author 38929
 * @date 2021年11月5日
 * @version 1.0
 * Email 1198865589@qq.com
 */
public class LoginUI {

	protected Shell shell;
	private Text text_user;
	private Text text_pwd;
	private Combo combo;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			LoginUI window = new LoginUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell( SWT.NONE | SWT.MIN ); //允许窗口最小化 和 关闭  SWT.MIN | SWT.CLOSE
		//shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN)); 
		//TODO 如何 最小化到托盘中 
		
		//设置界面的logo小图标 同时在项目结构中出现  SWTResourceManager类
		shell.setImage(SWTResourceManager.getImage(LoginUI.class, "/com/yc/images/小图标鱼.PNG"));
		shell.setSize(717, 411);
		shell.setText("南华客栈");
		
		
		//界面居中显示
		SWTUtil.centerWindow(shell);
		
		//1、给父容器设置填充式布局   子容器或组件占据整个父容器大小  适用于 面板  Composite 
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		//面板  不能独自打开界面  必须依托于主界面
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));    
		composite.setBackgroundMode(SWT.INHERIT_FORCE); // 指定子容器不去继承父容器的背景设定  透明化 

		
		//设置容器的背景图片    默认平铺效果
		composite.setBackgroundImage( SWTResourceManager.getImage(LoginUI.class, "/com/yc/images/登陆界面.PNG") );
		
		//TODO 如何背景图片的自适应  
		SWTUtil.imagetSize(composite);
		
		
		//创建其他组件 
		//绝大多数的 SWT 组件创建的时候都必须指定父组件和风格属性， 也就是必须有一个 parent 对象， 也不能运行的时候动态变更父组件
		//创建一个标签 账号
		Label label_user = new Label(composite, SWT.NONE); //默认样式
		label_user.setAlignment(SWT.CENTER);
		label_user.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 11, SWT.BOLD));
		label_user.setLocation( 379,  145);//x  y  设置相对父容器的位置
		label_user.setSize(100, 27);
		label_user.setText("账号:");
		
		//创建文本标签 可以录入用户信息  input
		Text text_user = new Text(composite, SWT.BORDER | SWT.NONE); //默认样式
		text_user.setLocation( 485,  144);//x  y  设置相对父容器的位置
		text_user.setSize(133, 28);
		text_user.setText("李卓");
		
		//创建文本标签 可以录入用户信息  input  密码裸露  需要掩盖
		Text text_pwd = new Text(composite, SWT.BORDER | SWT.PASSWORD); //默认样式   PASSWORD 密码框
		text_pwd.setLocation( 485,  196);//x  y  设置相对父容器的位置
		text_pwd.setSize(133, 27);
		text_pwd.setText("123321a");
		
		Combo combo = new Combo(composite, SWT.READ_ONLY);
		combo.setItems(new String[] {"会员", "管理员"});
		combo.setBounds(485, 241, 133, 28);
		combo.select(0);
		
		Button button_1 = new Button(composite, SWT.CENTER);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String name = text_user.getText().trim();
				String pwd = text_pwd.getText().trim();
				String type = combo.getText().trim();
				if(StringUtil.checkNull(name, pwd)) {
					StringUtil.showMessageBox(shell, "账号或密码不能为空");
					return ;
				}
				LoginIDao lg = new LoginDao();
				Map<String, Object> map = lg.Login(type, name, pwd);
				if(map == null) {
					StringUtil.showMessageBox(shell, "密码或账号错误，请检查后重新输入");
					return ;
				}
				if("管理员".equals(type)) {
					shell.dispose();
					Manager ma = new Manager();
					ma.open();
					
				}else if("会员".equals(type)) {
					shell.dispose();
					MainUI ma = new MainUI(map);
					ma.open();
				}
				
			}
		});
		
		button_1.setText("立即登录");
		button_1.setBounds(405, 300, 88, 27);
		
		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
		lblNewLabel.setAlignment(SWT.CENTER);
		lblNewLabel.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 15, SWT.BOLD | SWT.ITALIC));
		lblNewLabel.setBounds(327, 10, 291, 39);
		lblNewLabel.setText("欢迎来到摸鱼客栈");
		
		Label lblNewLabel_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 11, SWT.BOLD | SWT.ITALIC));
		lblNewLabel_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
		lblNewLabel_1.setAlignment(SWT.CENTER);
		lblNewLabel_1.setBounds(327, 50, 291, 28);
		lblNewLabel_1.setText("不要因为旅途的匆匆");
		
		Label label_user_1 = new Label(composite, SWT.NONE);
		label_user_1.setText("密码");
		label_user_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 11, SWT.BOLD));
		label_user_1.setAlignment(SWT.CENTER);	
		label_user_1.setBounds(379, 196, 100, 27);
		
		Label lblNewLabel_1_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1_1.setText("错过沿路的风景");
		lblNewLabel_1_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
		lblNewLabel_1_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 11, SWT.BOLD | SWT.ITALIC));
		lblNewLabel_1_1.setAlignment(SWT.CENTER);
		lblNewLabel_1_1.setBounds(457, 79, 161, 33);
		
		Button button_1_1 = new Button(composite, SWT.CENTER);
		button_1_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				RegisterUI re = new RegisterUI(shell, 0);
				re.open();
			}
		});
		button_1_1.setText("注册会员");
		button_1_1.setBounds(556, 300, 88, 27);
		
	}
	
}
